import { Controller } from "../../mvc/core/Controller";
import { ILuckyBagModule } from "../../../modules/interfaces/ILuckyBagModule";
import { IView } from "../../mvc/interfaces/IView";
import { LuckyBagModule } from "../../../modules/LuckyBagModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
import { SLuckyBagItem } from "../../../Message/Public/SLuckyBagItem";
import { Log } from "../../../com/gengine/debug/Log";
import { TodayNotTipUtil } from "../../common/TodayNotTipUtil";
import { TodayNoTipsConst } from "../../component/gconst/TodayNoTipsConst";
import { Alert } from "../../../com/mui/controls/Alert";
type int = number;
//class LuckyBagController
    
    export  class LuckyBagController extends Controller
    {
       

        public get luckyBag(): ILuckyBagModule
        {
            if (null == this._luckyBag) 
            {
                this.initView();
            }
            return this._luckyBag;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (this._luckyBag == null) 
            {
                this._luckyBag = new LuckyBagModule();
            }
            return this._luckyBag;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.LuckyBagOpentouse, this.onLuckyBagOpentouse);
            Dispatcher.addEventListener(EventName.LuckyBagOpentouseSuccess, this.onLuckyBagOpentouseSuccess);
            Dispatcher.addEventListener(EventName.LuckyBagClickGetItem, this.onLuckyBagClickGetItem);
            Dispatcher.addEventListener(EventName.LuckyBagClickGetItemSuccess, this.onLuckyBagClickGetItemSuccess);
            Dispatcher.addEventListener(EventName.LuckyBagAgainStart, this.onLuckyBagAgainStart);
            Dispatcher.addEventListener(EventName.LuckyBagBroadCastOpen, this.onLuckyBagBroadCastOpen);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onBackPackChangeHandler);
            return;
        }

        /* internal  */onLuckyBagBroadCastOpen(arg1: DataEvent): void
        {
            var loc1=cache.pack.backPackCache.getItemsByJudge(ItemsUtil.isLuckyBag, true);
            if (loc1.length <= 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(72004));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.LuckyBagOpentouse, (loc1[0] as ItemData).uid));
            }
            return;
        }

        /* internal  */onLuckyBagAgainStart(arg1: DataEvent): void
        {
            GameProxy.luckyBag.openLuckyBag(arg1.data as string);
            return;
        }

        /* internal  */onBackPackChangeHandler(arg1: Object=null): void
        {
            if (this._luckyBag && !this._luckyBag.isHide) 
            {
                this.luckyBag.updateLuckyBagLeft();
            }
            return;
        }

        /* internal  */onLuckyBagClickGetItemSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data.hadGetTimes as int;
            var loc2=arg1.data.bagItem as SLuckyBagItem;
            if (this.luckyBag && !this.luckyBag.isHide) 
            {
                this.luckyBag.openLuckyItem(loc1, loc2);
            }
            return;
        }

        /* internal  */onLuckyBagClickGetItem(arg1: DataEvent): void
        {
            if (cache.luckyBag.curLuckyBagUid == "") 
            {
                Log.system("福星袋点击卡牌，UID为空不能调接口");
            }
            else 
            {
                GameProxy.luckyBag.getLuckyItem(cache.luckyBag.curLuckyBagUid, arg1.data as string);
            }
            return;
        }

        /* internal  */onLuckyBagOpentouseSuccess(arg1: DataEvent): void
        {
            this.luckyBag.openLuckyBag(arg1.data as Map<any, any> /* flash.utils.Dictionary */);
            this.luckyBag.show();
            return;
        }

        /* internal  */onLuckyBagOpentouse(arg1: DataEvent): void
        {
            if (this.luckyBag.isHide) 
            {
                GameProxy.luckyBag.openLuckyBag(arg1.data as string);
            }
            else if (cache.luckyBag.hadGetTimes > 0) 
            {
                TodayNotTipUtil.toDayNotTip(TodayNoTipsConst.LuckyBagOpenandPackUse, this.replaceRequest, Language.getString(72003), null, Alert.OK | Alert.CANCEL);
            }
            else 
            {
                GameProxy.luckyBag.openLuckyBag(arg1.data as string);
            }
            return;
        }

        /* internal  */replaceRequest(): void
        {
            GameProxy.luckyBag.openLuckyBag(cache.luckyBag.curLuckyBagUid);
            return;
        }

        private /* var */_luckyBag: ILuckyBagModule;
    }
